package cn.xopencode.userservice.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * 静态方法工具类
 *
 * @Author xiaodawei
 * @since 2021/9/18
 */
public final class FunUtil {


    public static <T> T defaultValue(T obj, String userId, Boolean isCreate) {
        Class<T> objClass = (Class<T>) obj.getClass();
        try {
            Method setCreatorId = objClass.getMethod("setCreatorId", String.class);
            Method setUpdateId = objClass.getMethod("setUpdateId", String.class);
            Method setCreateTime = objClass.getMethod("setCreateTime", Date.class);
            Method setUpdateTime = objClass.getMethod("setUpdateTime", Date.class);
            if (isCreate) {
                setCreatorId.invoke(obj, userId);
                setCreateTime.invoke(obj, DateUtil.date());
            }
            setUpdateId.invoke(obj, userId);
            setUpdateTime.invoke(obj, DateUtil.date());
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return obj;
    }

    public static Boolean mustOne(String... str) {
        for (int i = 0; i < str.length; i++) {
            if (StrUtil.isNotBlank(str[i]))
                return true;
        }
        return false;
    }
}
